package com.jxbeidou.generate.controller;

import com.jxbeidou.core.constents.ValidateConstants;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.core.validate.ModifyValidateGroup;
import com.jxbeidou.core.validate.SaveValidateGroup;
import com.jxbeidou.generate.dto.FieldSaveDTO;
import com.jxbeidou.generate.dto.FieldStepUpdateDTO;
import com.jxbeidou.generate.query.FieldQueryDTO;
import com.jxbeidou.generate.query.page.FieldPageQueryDTO;
import com.jxbeidou.generate.service.FieldService;
import com.jxbeidou.generate.vo.FieldVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: jxbeidou-basic
 * @ClassName: FieldController
 * @description: 字段管理
 * @author: 杜晓祥
 * @create: 2024-03-13 11:36
 * 版权所有：交信北斗(北京)信息科技有些公司
 */
@Slf4j
@RestController
@RequestMapping("/generate/field")
@Tag(name = "字段管理")
public class FieldController {
    
    @Resource
    private FieldService fieldService;


    @PostMapping("/page")
    @Operation(summary = "分页查询")
    public R<PageResult<FieldVO>> page(@RequestBody @Valid FieldPageQueryDTO query){
        PageResult<FieldVO> page = fieldService.page(query);
        return R.success(page);
    }

    @PostMapping("/list")
    @Operation(summary = "查询所有复合条件的数据")
    public R<List<FieldVO>> list (@RequestBody @Valid FieldQueryDTO query) {
        List<FieldVO> list = fieldService.list(query);
        return R.success(list);
    }

    @PutMapping("/stepUpdate")
    @Operation(summary = "更新当前字段信息至缓存中")
    public R<String> stepUpdate (@RequestBody @Valid FieldStepUpdateDTO params) {
        fieldService.stepFieldForCache(params);
        return R.success();
    }

    @PutMapping("/persistence/{key}")
    @Operation(summary = "数据持久化,关联stepUpdate接口")
    public R<String> stepWriter (@PathVariable @NotEmpty(message = "唯一 KEY 不能为空") String key) {
        fieldService.stepFieldModify(key);
        return R.success();
    }

    @GetMapping("/existsKey/{key}")
    public R<Boolean> exists (@PathVariable @NotEmpty(message = "唯一 KEY 不能为空") String key) {
        return R.success(fieldService.exists(key));
    }

    @GetMapping("/{id}")
    @Operation(summary = "ID查询")
    public R<FieldVO> get(@PathVariable("id") @NotNull(message = ValidateConstants.FIND_ID_NULL) Long id){
        return R.success(fieldService.findById(id));
    }

    @PostMapping
    @Operation(summary = "新增")
    public R<String> save(@RequestBody @Validated(SaveValidateGroup.class) FieldSaveDTO vo){
        fieldService.save(vo);
        return R.saveSuccess();
    }

    @PutMapping
    @Operation(summary = "修改")
    public R<String> update(@RequestBody @Validated(ModifyValidateGroup.class) FieldSaveDTO vo){
        fieldService.modify(vo);
        return R.modifySuccess();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "ID删除")
    public R<String> delete(@PathVariable("id") @NotNull(message = ValidateConstants.DELETE_ID_NULL) Long id){
        fieldService.delete(id);
        return R.deleteSuccess();
    }

    @PutMapping("/delete")
    @Operation(summary = "批量删除")
    public R<String> delete(@RequestBody @NotEmpty(message = ValidateConstants.DELETE_IDS_NULL) List<Long> idList) {
        fieldService.deleteForList(idList);
        return R.deleteSuccess();
    }
    
}
